/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package system.database.mysql5;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.aionemu.commons.database.DatabaseFactory;
import com.aionemu.gameserver.dao.PlayerEmotionListDAO;
import com.aionemu.gameserver.model.gameobjects.player.Player;

/**
 * @author Mr. Poke
 *
 */
public class MySQL5PlayerEmotionList extends PlayerEmotionListDAO
{
	/** Logger */
	private static final Logger	log				= Logger.getLogger(PlayerEmotionListDAO.class);

	public static final String	INSERT_QUERY	= "INSERT INTO `player_emotions` (`player_id`, `emotion`) VALUES (?,?)";
	public static final String	SELECT_QUERY	= "SELECT `emotion` FROM `player_emotions` WHERE `player_id`=?";
	
	/* (non-Javadoc)
	 * @see com.aionemu.commons.database.dao.DAO#supports(java.lang.String, int, int)
	 */
	@Override
	public boolean supports(String databaseName, int majorVersion, int minorVersion)
	{
		return MySQL5DAOUtils.supports(databaseName, majorVersion, minorVersion);
	}

	/* (non-Javadoc)
	 * @see com.aionemu.gameserver.dao.PlayerEmotionListDAO#loadEmotions(com.aionemu.gameserver.model.gameobjects.player.Player)
	 */
	@Override
	public void loadEmotions(Player player)
	{
		Connection con = null;
		try
		{
			con = DatabaseFactory.getConnection();
			PreparedStatement stmt = con.prepareStatement(SELECT_QUERY);
			stmt.setInt(1, player.getObjectId());
			ResultSet rset = stmt.executeQuery();
			List<Integer> emotions = new ArrayList<Integer>();
			while (rset.next())
			{
				emotions.add(rset.getInt("emotion"));
				
			}
			if (!emotions.isEmpty())
				player.setEmotions(emotions);
			rset.close();
			stmt.close();
		}
		catch (Exception e)
		{
			log.fatal("Could not restore emotionId for playerObjId: " + player.getObjectId() + " from DB: " + e.getMessage(), e);
		}
		finally
		{
			DatabaseFactory.close(con);
		}
	}

	/* (non-Javadoc)
	 * @see com.aionemu.gameserver.dao.PlayerEmotionListDAO#insertEmotion(com.aionemu.gameserver.model.gameobjects.player.Player, int)
	 */
	@Override
	public void insertEmotion(Player player, int emotionId)
	{
		Connection con = null;
		try
		{
			con = DatabaseFactory.getConnection();
			PreparedStatement stmt = con.prepareStatement(INSERT_QUERY);
			stmt.setInt(1, player.getObjectId());
			stmt.setInt(2, emotionId);
			stmt.execute();
			stmt.close();
		}
		catch (Exception e)
		{
			log.fatal("Could not store emotionId for player " + player.getObjectId() + " from DB: " + e.getMessage(), e);
		}
		finally
		{
			DatabaseFactory.close(con);
		}
	}

}
